package com.huya.service.impl;

import com.huya.mapper.ReportMapper;
import com.huya.mapper.UserMapper;
import com.huya.pojo.vo.ReportDataVO;
import com.huya.pojo.vo.UserDataVO;
import com.huya.service.DataService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class DataServiceImpl implements DataService {

    @Autowired
    private UserMapper userMapper;
    @Autowired
    private ReportMapper reportMapper;
    /**
     * 统计指定时间段内的用户数据
     * @param begin
     * @param end
     * @return
     */
    public UserDataVO getUserData(LocalDate begin, LocalDate end) {
        //存放begin到end每天的对应的日期
        List<LocalDate> dateList = new ArrayList<>();
        dateList.add(begin);
        while (!begin.equals(end)) {
            begin = begin.plusDays(1);
            dateList.add(begin);
        }
        List<Integer> newUserList = new ArrayList<>();
        List<Integer> totalUserList = new ArrayList<>();
        for (LocalDate date : dateList) {
            LocalDateTime beginTime = LocalDateTime.of(date, LocalTime.MIN);
            LocalDateTime endTime = LocalDateTime.of(date, LocalTime.MAX);
            Map map = new HashMap();
            map.put("end", endTime);
            //总用户数
            Integer totalUser = userMapper.countByMap(map);

            map.put("begin", beginTime);
            //新增用户数
            Integer newUser = userMapper.countByMap(map);

            totalUserList.add(totalUser);
            newUserList.add(newUser);
        }
        return UserDataVO
                .builder()
                .dateList(StringUtils.join(dateList, ","))
                .totalUserList(StringUtils.join(totalUserList, ","))
                .newUserList(StringUtils.join(newUserList, ","))
                .build();
    }

    /**
     * 统计指定时间段内的举报数据
     * @param begin
     * @param end
     * @return
     */
    public ReportDataVO getReportData(LocalDate begin, LocalDate end) {
        //存放begin到end每天的对应的日期
        List<LocalDate> dateList = new ArrayList<>();
        dateList.add(begin);
        while (!begin.equals(end)) {
            begin = begin.plusDays(1);
            dateList.add(begin);
        }

        //获取每天的举报信数量和已完成的举报信数量
        List<Integer> reportList = new ArrayList<>();
        List<Integer> totalReportList = new ArrayList<>();
        for (LocalDate date : dateList) {
        //获取当天的举报信数量
            LocalDateTime beginTime = LocalDateTime.of(date, LocalTime.MIN);
            LocalDateTime endTime = LocalDateTime.of(date, LocalTime.MAX);
            Map map = new HashMap();
            map.put("begin", beginTime);
            map.put("end", endTime);
            Integer reportCount = reportMapper.countByMap(map);
            //获取当天的已完成的举报信数量
            map.put("status", 2);
            Integer totalReport = reportMapper.countByMap(map);
            reportList.add(reportCount);
            totalReportList.add(totalReport);
        }
        //计算时间段内的总举报信数量
        Integer totalReport = reportList.stream().reduce(Integer::sum).get();
        //计算时间段内的总已完成的举报信数量
        Integer completedReport = totalReportList.stream().reduce(Integer::sum).get();
        return ReportDataVO.builder()
               .dateList(StringUtils.join(dateList, ","))
               .reportList(StringUtils.join(reportList, ","))
               .completedReportList(StringUtils.join(totalReportList, ","))
                .totalReport(totalReport)
               .completedReport(completedReport)
               .build();

    }
}
